package cn.edu.zjicm.wordsnet_d.activity;

import com.umeng.analytics.MobclickAgent;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.synchronize.ForgetPwdInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.DataValidator;

public class ChangePwdWithCodeActivity extends WordBaseActivity implements OnClickListener {
	EditText pwd1;
	Button commit;
	TextView showPwd;
	String loginId, code;
	ProgressDialog dialog;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what != ForgetPwdInterface.MSG_START) {
				if (dialog != null) {
					dialog.dismiss();
					dialog = null;
				}
			}
			switch (msg.what) {
			case ForgetPwdInterface.MSG_START:
				dialog = ProgressDialog.show(ChangePwdWithCodeActivity.this, null, "正在修改...");
				break;
			case ForgetPwdInterface.MSG_SUCCESS:
				LoginActivity.startActivity(ChangePwdWithCodeActivity.this, false, loginId);
				// Toast.makeText(ChangePwdWithCodeActivity.this, "修改密码成功",
				// Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_UNSUCCESS:
				Toast.makeText(ChangePwdWithCodeActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_BAD_NETWORK:
				Toast.makeText(ChangePwdWithCodeActivity.this, UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(ChangePwdWithCodeActivity.this, UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("忘记密码");
		setContentView(R.layout.activity_new_pwd);

		setBackButton(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LoginActivity.startActivity(ChangePwdWithCodeActivity.this, false, loginId);
			}
		});

		loginId = getIntent().getStringExtra("loginId");
		code = getIntent().getStringExtra("code");

		pwd1 = (EditText) findViewById(R.id.newPwd_pwd);
		pwd1.addTextChangedListener(textWatcher);
		
		showPwd = (TextView) findViewById(R.id.show_new_pwd);
		showPwd.setOnTouchListener(touchListener);
		
		commit = (Button) findViewById(R.id.newPwdBtn);
		commit.setOnClickListener(this);
	}

	protected void toCommit() {
		String pwd1Text = pwd1.getText().toString();
		if (!DataValidator.PwdIsValidate(pwd1Text)) {
			Toast.makeText(ChangePwdWithCodeActivity.this, "密码格式错误，请输入6~16位数字或字母", Toast.LENGTH_SHORT).show();
		} else {
			handler.sendMessage(handler.obtainMessage(ForgetPwdInterface.MSG_START));
			ForgetPwdInterface.getInstance(ChangePwdWithCodeActivity.this).toChangePwdWithCode(loginId, code, pwd1Text, handler);
		}
	}

	OnTouchListener touchListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				pwd1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				pwd1.setSelection(pwd1.getText().length());
				break;

			case MotionEvent.ACTION_UP:
				pwd1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
				pwd1.setSelection(pwd1.getText().length());
				break;
			}
			return true;
		}
	};
	
	TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
			checkInfo();
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

	};

	private void checkInfo() {
		boolean flag = true;
		if (pwd1.hasFocus()) {
			if (DataValidator.EditIsNull(pwd1.getText().toString())) {
				flag = false;
			}
		}
		if (!flag) {
			commit.setEnabled(false);
		} else {
			commit.setEnabled(true);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newPwdBtn:
			toCommit();
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 当列表为根节点时，确认是否退出
		boolean consumed = false;
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			LoginActivity.startActivity(ChangePwdWithCodeActivity.this, false, loginId);
			break;
		}
		return consumed;
	}
}